package org.classg.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.classg.servicebase.exception.MyException;
import org.classg.ucenter.entity.Member;
import org.classg.ucenter.entity.vo.LoginVo;
import org.classg.ucenter.entity.vo.RegisterVo;
import org.classg.ucenter.mapper.MemberMapper;
import org.classg.ucenter.service.MemberService;
import org.classg.utils.JwtUtils;
import org.classg.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-04
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 会员登录
    @Override
    public String login(LoginVo loginVo) {
        System.out.println("密码123123："+MD5.encrypt("123123"));
        // 判断账号或密码是否为空
        if (StringUtils.isEmpty(loginVo.getMobile()) || StringUtils.isEmpty(loginVo.getPassword())) {
            throw new MyException(20001, "账号或密码为空！");
        }
        // 判断账号是否存在
        Member member = baseMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getMobile, loginVo.getMobile()));
        if (null == member) {
            throw new MyException(20001, "账号不存在！");
        }
        // 校验密码是否正常
        if (!MD5.encrypt(loginVo.getPassword()).equals(member.getPassword())) {
            throw new MyException(20001, "密码不正确！");
        }
        // 查看用户是否被禁用
        if (member.getIsDisabled()) {
            throw new MyException(20001, "用户被禁用，请联系管理员！");
        }
        // 使用JWT生成token字符串
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    // 会员注册
    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        String code = registerVo.getCode();
        // 判断账号或密码或验证码或名称为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)) {
            throw new MyException(20001, "账号或密码或验证码或名称为空！");
        }
        // 查看redis中的redis是否正确
        String redisCode = (String) redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new MyException(20001, "验证码错误！");
        }
        // 查看数据库中是否有相同的手机号码
        Integer temp = baseMapper.selectCount(new LambdaQueryWrapper<Member>().eq(Member::getMobile, mobile));
        if (temp > 0) {
            throw new MyException(20001, "手机号已经注册过了！");
        }
        // 添加信息到数据库
        Member member = new Member();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setAvatar("https://jiyue-edu.oss-cn-shenzhen.aliyuncs.com/2020/05/29/29fa51de4f4c48eba5e488c7b5ead548file.png");
        member.setIsDisabled(false);
        baseMapper.insert(member);
    }

    // 根据微信唯一标识openid查询用户
    @Override
    public Member queryUserByOpenId(String openid) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Member>().eq(Member::getOpenid, openid));
    }

    // 根据天数查询注册人数
    @Override
    public Integer queryRegisterCountByDay(String day) {
        return baseMapper.queryRegisterCountByDay(day);
    }
}
